<?php

namespace app\asking\controller;

use think\Controller;

use app\common\library\Email;

class Business extends Controller
{
    public function __construct(){
        parent::__construct();

        $this->BusinessModel = model('Business.User');
        $this->PostModel = model('Post.Post');
        $this->CommentModel = model('Post.Comment');
        $this->CollectModel = model('Post.Collect');
        $this->FansModel = model('Business.Fans');
        $this->MsgModel = model('Business.Msg');

        $this->PayModel = model('Pay.Pay');
    }

    // 微信授权登录
    public function login(){
        if($this->request->isPost()){
            $code = $this->request->param('code','','trim');

            if(empty($code)){
                $this->error('临时凭证获取失败');
                exit;
            }

            // 发送请求给微信端
            $wxauth = $this->code2Session($code);

            $openid = isset($wxauth['openid']) ? trim($wxauth['openid']) : '';

            if(empty($openid)){
                $this->error('授权失败');
                exit;
            }

            // 根据openid查找是否存在用户
            $business = $this->BusinessModel->where(['openid' => $openid])->find();

            if($business){
                unset($business['password']);
                unset($business['salt']);

                // 授权成功
                $this->success('授权登录成功',null,$business);
                exit;
            }else{
                $this->success('授权成功，请绑定账号',"/pages/business/login",['action' => 'bind','openid' => $openid]);
                exit;
            }
        }
    }


    // 微信端绑定
    public function bind(){
        if($this->request->isPost()){
            $openid = $this->request->param('openid','','trim');
            $password = $this->request->param('password','','trim');
            $mobile = $this->request->param('mobile','','trim');

            // 查找用户
            $business = $this->BusinessModel->where(['mobile'=>$mobile])->find();

            // 如果找的到就说明绑定过，找不到说明不存在就执行注册插入
            if($business){
                // 更新语句
                if(!empty($business['openid'])){
                    $this->error('该用户已经绑定，无法重复绑定');
                    exit;
                }

                $data = [
                    'id' => $business['id'],
                    'openid' => $openid
                ];

                // 更新语句
                $result = $this->BusinessModel->isUpdate(true)->save($data);

                if($result === FALSE){
                    $this->error('账号绑定失败');
                    exit;
                }else{
                    $business['openid'] = $openid;
                    unset($business['salt']);
                    unset($business['password']);
                    $this->success('绑定账号成功',null,$business);
                    exit;
                }
            }else{
                // 数据插入
                if(empty($password)){
                    $this->error('密码不能为空');
                    exit;
                }

                // 生成密码盐
                $salt = randstr();

                // 加密密码
                $password = md5($password.$salt);

                // 组装数据
                $data = [
                    'openid' => $openid,
                    'mobile' => $mobile,
                    'nickname' => $mobile,
                    'password' => $password,
                    'salt' => $salt,
                    'gender' => '0',
                    'deal' => '0',
                    'money' => '0',
                    'auth' => '0'
                ];

                // 查询出知识问答社区来源的id
                $data['sourceid'] = model('common/Business/Source')->where(['name'=>['LIKE',"%知识问答社区%"]])->value('id');

                // 执行插入
                $result = $this->BusinessModel->validate('common/Business/Business')->save($data);

                if($result === FALSE){
                    $this->error($this->BusinessModel->getError());
                    exit;
                }else{
                    // 查询出当前插入记录
                    $business = $this->BusinessModel->find($this->BusinessModel->id);

                    unset($business['password']);
                    unset($business['salt']);

                    $this->success('注册成功',null,$business);
                    exit;
                }
            }
        }
    }

    // web注册登录方式
    public function web(){
        if($this->request->isPost()){
            $password = $this->request->param('password','','trim');
            $mobile = $this->request->param('mobile','','trim');

            // 查找用户
            $business = $this->BusinessModel->where(['mobile'=>$mobile])->find();

            // 如果找的到就说明绑定过，找不到说明不存在就执行注册插入
            if($business){
                // 验证密码是否正确
                $salt = $business['salt'];
                $password = md5($password.$salt);

                if($password != $business['password']){
                    $this->error('密码错误');
                    exit;
                }else{
                    unset($business['salt']);
                    unset($business['password']);
                    $this->success('登录成功',null,$business);
                    exit;
                }
            }else{
                // 数据插入
                if(empty($password)){
                    $this->error('密码不能为空');
                    exit;
                }

                // 生成密码盐
                $salt = randstr();

                // 加密密码
                $password = md5($password.$salt);

                // 组装数据
                $data = [
                    'mobile' => $mobile,
                    'nickname' => $mobile,
                    'password' => $password,
                    'salt' => $salt,
                    'gender' => '0',
                    'deal' => '0',
                    'money' => '0',
                    'auth' => '0'
                ];

                // 查询出知识问答社区来源的id
                $data['sourceid'] = model('common/Business/Source')->where(['name'=>['LIKE',"%知识问答社区%"]])->value('id');

                // 执行插入
                $result = $this->BusinessModel->validate('common/Business/Business')->save($data);

                if($result === FALSE){
                    $this->error($this->BusinessModel->getError());
                    exit;
                }else{
                    // 查询出当前插入记录
                    $business = $this->BusinessModel->find($this->BusinessModel->id);

                    unset($business['password']);
                    unset($business['salt']);

                    $this->success('注册成功',null,$business);
                    exit;
                }
            }
        }
    }


    // 调用微信官方获取用户信息
    public function code2Session($code)
    {

       if($code) {

            // 改成自己的小程序 appid
            $appid = "wxf54c758aefb06b42";

            // 改成自己的小程序 appSecret
            $appSecret="91167b374e820bfceb16c6fd7832a59b";

            // 微信官方提供的接口，获取唯一的opendid
            $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$appSecret&js_code=$code&grant_type=authorization_code";

            $result = $this->https_request($url);

            //获取结果 将json转化为数组
            $resultArr = json_decode($result,true);

            return $resultArr;
       } 
       return false;

    }


    //http请求 利用php curl扩展去发送get 或者 post请求 服务器上面一定要开启 php curl扩展
    // https://www.php.net/manual/zh/book.curl.php
    protected function https_request($url,$data = null)
    {
        if(function_exists('curl_init')){
        $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
            if (!empty($data)){
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            // 发送会话，返回结果
            $output = curl_exec($curl);
            curl_close($curl);
            return $output;
        }else{
            return false;
        }
    }


    // 更新资料
    public function profile(){
        if($this->request->isPost()){
            $id = $this->request->param('id',0,'trim');
            $mobile = $this->request->param('mobile','','trim');
            $nickname = $this->request->param('nickname','','trim');
            $email = $this->request->param('email','','trim');
            $gender = $this->request->param('gender',0,'trim');
            $code = $this->request->param('code','','trim');
            $password = $this->request->param('password','','trim');
            $intro = $this->request->param('intro','','trim');

            // 查询用户
            $business = $this->BusinessModel->find($id);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 组装数据
            $data = [
                'id' => $business['id'],
                'nickname' => $nickname,
                'mobile' => $mobile,
                'gender' => $gender
            ];

            // 如果密码不为空
            if(!empty($password)){
                $salt = randstr();

                $data['salt'] = $salt;
                $data['password'] = md5($password.$salt);
            }

            // 如果个人介绍不为空
            if(!empty($intro)){
                $data['intro'] = $intro;
            }

            // 判断是否修改了邮箱
            if($email != $business['email']){
                $data['email'] = $email;
                $data['auth'] = 0;
            }

            // 判断是否有地区编码
            if(!empty($code)){
                // 查询省市区的地区码 
                $parent = model('Region')->where(['code' => $code])->value('parentpath');

                if(!empty($parent)){
                    $arr = explode(',',$parent);
                    $data['province'] = isset($arr[0]) ? $arr[0] : null;
                    $data['city'] = isset($arr[1]) ? $arr[1] : null;
                    $data['district'] = isset($arr[2]) ? $arr[2] : null;
                }
            }

            // 判断是否有图片上传
            if(isset($_FILES['avatar']) && $_FILES['avatar']['error'] == 0){
                $success = build_upload('avatar');

                // 如果上传失败，就提醒
                if(!$success['result']){
                    $this->error($success['msg']);
                    exit;
                }

                // 如果上传成功
                $data['avatar'] = $success['data'];
            }

            $result = $this->BusinessModel->validate('common/Business/Business.profile')->isUpdate(true)->save($data);

            if($result === FALSE){
                $this->error('更新失败');
                exit;
            }else{
                // 判断是否有旧图片
                if(isset($data['avatar'])){
                    is_file(".".$business['avatar']) && @unlink(".".$business['avatar']);
                }

                $business = $this->BusinessModel->find($id);
                
                unset($business['password']);
                unset($business['salt']);

                $this->success('更新资料成功',null,$business);
                exit;

            }
        }
    }


    // 邮箱验证码
    public function email(){
        if($this->request->isPost()){
            $EmsModel = model('common/Ems');
            $id = $this->request->param('id',0,'trim');
            $action = $this->request->param('action','','trim');
            // var_dump($this->request->param());
            // exit;
            // 查询用户
            $business = $this->BusinessModel->find($id);
            // var_dump($business);
            // exit;
            if(!$business){
                $this->error('用户未知');
                exit;
            }
            
            $email = !empty($business['email']) ? $business['email'] : ''; 
            if(empty($email)){
                $this->error('用户邮箱未知');
                exit;
            }

            if($action == 'send'){
                // 生成一个验证码
                $code = randstr(5);

                // 开启事务
                $EmsModel->startTrans();

                // 删除掉之前旧的验证码
                $EmsModel->where(['email'=> $email])->delete();

                // 把验证码插入到数据库表中
                $data = [
                    'event' => 'auth',
                    'email' => $email,
                    'code' => $code,
                    'times' => 0
                ];

                // 插入数据
                $ems = $EmsModel->save($data);

                if($ems === FALSE){
                    $this->error('验证码插入失败');
                    exit;
                }
                // 邮件主题
                $name = config('site.name');
                $subject = "【{$name}】邮箱验证";

                // 组装文字信息
                $message = "<div>感谢您的使用，您的邮箱验证码为：<b>$code</b></div>";

                // 实例化邮箱验证类
                $PhpMailer = new Email;

                // 邮箱发送有规律，不可以发送关键词
                $result = $PhpMailer
                    ->to($email)
                    ->subject($subject)
                    ->message($message)
                    ->send();

                // 检测邮箱发送成功还是失败
                if($result){
                    // 发送验证码成功
                    // 将事务提交，提交的意思是让刚插入的记录真实存在到数据表中
                    $EmsModel->commit();
                    $this->success('邮件发送成功，请注意查收');
                    exit;
                }else{
                    // 将刚才插入成功的验证码记录撤销回滚
                    $EmsModel->rollback();
                    $this->error($PhpMailer->getError());
                    exit;
                }
            }else{
                $code = $this->request->param('code',0,'trim');
                if(empty($code)){
                    $this->error('验证码不能为空');
                    exit;
                }

                // 开启事务
                $EmsModel->startTrans();
                $this->BusinessModel->startTrans();
                
                // 查询这个验证码是否存在
                $where = [
                    'email' => $email,
                    'code' => $code
                ];

                $check = $EmsModel->where($where)->find();

                if(!$check){
                    $this->error('您输入的验证码有误，请重新输入');
                    exit;
                }

                // 更新用户表
                $BusinessData = [
                    'id' => $business['id'],
                    'auth' => 1
                ];

                $BusinessStatus = $this->BusinessModel->isUpdate(true)->save($BusinessData);

                if($BusinessStatus === FALSE){
                    $this->error($this->BusinessModel->getError());
                    exit;
                }

                $EmsStatus = $EmsModel->where($where)->delete();
                if($EmsStatus === FALSE){
                    $this->BusinessModel->rollback();
                    $this->error($EmsModel->getError());
                    exit;
                }

                if($EmsStatus === FALSE || $BusinessStatus === FALSE){
                    $EmsModel->rollback();
                    $this->BusinessModel->rollback();
                    $this->error('邮箱验证失败');
                    exit;
                }else{
                    $EmsModel->commit();
                    $this->BusinessModel->commit();
                    $business = $this->BusinessModel->find($id);
                    $this->success('邮箱验证成功',null,$business);
                    exit;
                }
            }
        }
    }


    // 解绑账号
    public function bindout(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            $data = [
                'id' => $busid,
                'openid' => NULL
            ];

            $result = $this->BusinessModel->isUpdate(true)->save($data);

            if($result === FALSE){
                $this->error('解绑账号失败，请重试');
                exit;
            }else{
                $this->success('解绑账号成功，并退出登录');
                exit;
            }
        }
    }


    // 我的主页请求数据
    public function data(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $tabid = $this->request->param('tabid',0,'trim');
            $page = $this->request->param('page',1,'trim');
            $limit = 10;
            $offset = ($page-1) * $limit;

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 我的提问
            if($tabid == 0){
                $list = $this->PostModel
                    ->with(['business','cate'])
                    ->where(['busid'=>$busid])
                    ->limit($offset,$limit)
                    ->select();

                if(empty($list)){
                    $this->error('暂无更多提问');
                    exit;
                }else{
                    $this->success('返回提问',null,$list);
                    exit;
                }
            }

            // 我的回答
            if($tabid == 1){
                $postid = $this->CommentModel->where(['busid' => $busid])->column('postid');

                // var_dump($postid);
                // exit;

                $list = $this->PostModel
                    ->with(['business','cate'])
                    ->where(['post.id' => ['IN',$postid]])
                    ->limit($offset,$limit)
                    ->select();

                // var_dump($list);
                // exit;

                if(empty($list)){
                    $this->error('暂无更多回答');
                    exit;
                }else{
                    $this->success('返回回答',null,$list);
                    exit;
                }
            }

            // 我的收藏
            if($tabid == 2){
                $postid = $this->CollectModel->where(['busid'=>$busid])->column('postid');

                $list = $this->PostModel
                    ->with(['business','cate'])
                    ->where(['post.id' => ['IN',$postid]])
                    ->limit($offset,$limit)
                    ->select();
                
                if(empty($list)){
                    $this->error('暂无更多收藏');
                    exit;
                }else{
                    $this->success('返回收藏',null,$list);
                    exit;
                }
                
            }

        }
    }


    // 我的提问(我的主页)
    public function ask(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            $asklist = $this->PostModel
                    ->with(['business','cate'])
                    ->where(['busid' => $busid])
                    ->select();

            if(empty($asklist)){
                $this->error('暂无更多提问');
                exit;
            }else{
                $this->success('返回提问',null,$asklist);
                exit;
            }
        }
    }


    // 我的回答(我的主页)
    public function answer(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            $answerlist = $this->CommentModel
                    ->where(['busid'=>$busid])
                    ->select();

            if(empty($answerlist)){
                $this->error('暂无更多回答');
                exit;
            }else{
                $this->success('返回回答',null,$answerlist);
                exit;
            }
        }
    }


    // 我的收藏(我的主页)
    public function collect(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            $collectlist = $this->CollectModel->where(['busid'=>$busid])->select();

            if(empty($collectlist)){
                $this->error('暂无更多收藏');
                exit;
            }else{
                $this->success('返回收藏',null,$collectlist);
                exit;
            }
        }
    }


    // 我的提问
    public function myask(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $page = $this->request->param('page',1,'trim');
            $keywords = $this->request->param('keywords','','trim');
            $limit = 10;
            
            $offset = ($page-1)*$limit;

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            $where = [
                'busid' => $busid
            ];

            if($keywords){
                $where['title'] = ['like',"%$keywords%"];
            }

            $asklist = $this->PostModel
                    ->with(['business','cate'])
                    ->where($where)
                    ->limit($offset,$limit)
                    ->select();

            if(empty($asklist)){
                $this->error('暂无更多提问');
                exit;
            }else{
                $this->success('返回提问',null,$asklist);
                exit;
            }
        }
    }


    // 我的回答
    public function myanswer(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $page = $this->request->param('page',1,'trim');
            $keywords = $this->request->param('keywords','','trim');
            $limit = 10;
            
            $offset = ($page-1)*$limit;

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 查询回答
            $postid = $this->CommentModel->where(['busid' => $busid])->column('postid');

            // var_dump($postid);
            // exit;

            $where = [
                'post.id' => ['IN',$postid] 
            ];

            if($keywords){
                $where['title'] = ['like',"%$keywords%"];
            }


            $list = $this->PostModel
                ->with(['business','cate'])
                ->where($where)
                ->limit($offset,$limit)
                ->select();

            // var_dump($list);
            // exit;

            if(empty($list)){
                $this->error('暂无更多回答');
                exit;
            }else{
                $this->success('返回回答',null,$list);
                exit;
            }
        }
    }


    // 我的收藏
    public function mycollect(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $page = $this->request->param('page',1,'trim');
            $keywords = $this->request->param('keywords','','trim');
            $limit = 10;
            
            $offset = ($page-1)*$limit;

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 查询回答
            $postid = $this->CollectModel->where(['busid' => $busid])->column('postid');

            // var_dump($postid);
            // exit;

            $where = [
                'post.id' => ['IN',$postid] 
            ];

            if($keywords){
                $where['title'] = ['like',"%$keywords%"];
            }


            $list = $this->PostModel
                ->with(['business','cate'])
                ->where($where)
                ->limit($offset,$limit)
                ->select();

            // var_dump($list);
            // exit;

            if(empty($list)){
                $this->error('暂无更多收藏');
                exit;
            }else{
                $this->success('返回收藏',null,$list);
                exit;
            }
        }
    } 


    // 用户信息获取
    public function business(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');

            // 查找用户
            $business = $this->BusinessModel->find($busid);

            if($business){
                $this->success('返回用户信息',null,$business);
                exit;
            }else{
                $this->error('用户信息加载失败');
            }
        }
    }


    // 关注
    public function concern(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $concern = $this->request->param('concern',0,'trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('您的用户信息未知');
                exit;
            }

            $ConcernData = [
                'id' => $busid
            ];


            // 判断是否是他的粉丝
            $concernbus = $this->FansModel->where(['busid' => $concern])->find();


            // 开启事务
            $this->BusinessModel->startTrans();
            $this->FansModel->startTrans();

            // 判断是否关注过
            $concern_list = $business['concern_list'];
            $fans_list = $concernbus ? $concernbus['fans_list'] : [];

            // 判断元素是否在里面
            if(in_array($concern,$concern_list) && in_array($busid,$fans_list)){
                $FansData = [
                    'id' => $concernbus['id']
                ];

                // 在里面就要取消关注
                $conindex = array_search($concern,$concern_list);
                $fanindex = array_search($busid,$fans_list);
              
                unset($concern_list[$conindex]);
                unset($fans_list[$fanindex]);
               
                if(empty($concern_list)){
                    $ConcernData['concern'] = NULL; 
                }else{
                    $ConcernData['concern'] = implode(',',$concern_list);
                }


                if(empty($fans_list) && $concernbus){
                    $FansStatus = $this->FansModel->where(['id' => $concernbus['id']])->delete();
                }else{
                    if($concernbus){
                       $FansData['fans'] = implode(',',$fans_list); 
                       $FansStatus = $this->FansModel->isUpdate(true)->save($FansData);
                    }    
                }

                $ConcernStatus = $this->BusinessModel->isUpdate(true)->save($ConcernData);

                if($ConcernStatus === FALSE){
                    $this->error($this->BusinessModel->getError());
                    exit;
                }

                
                if($FansStatus === FALSE){
                    $this->BusinessModel->rollback();
                    $this->error($this->FansModel->getError());
                    exit;
                }

                if($ConcernStatus === FALSE || $FansStatus === FALSE){
                    $this->FansModel->rollback();
                    $this->BusinessModel->rollback();
                    $this->error('取消关注失败');
                    exit;
                }else{
                    $business = $this->BusinessModel->find($this->BusinessModel->id);
                    $this->FansModel->commit();
                    $this->BusinessModel->commit();
                    $this->success('取消关注成功',null,$business);
                    exit;
                }

            }else{
                // 不在里面就关注
                $concern_list[] = $concern;
                $fans_list[] = $busid;

                if(empty($concern_list)){
                    $ConcernData['concern'] = NULL; 
                }else{
                    $ConcernData['concern'] = implode(',',$concern_list);
                }

                $concernbus = $this->FansModel->where(['busid' => $concern])->find();

                if($concernbus){
                    $FansData = [
                        'id' => $concernbus['id'],
                        'fans' => implode(',',$fans_list)
                    ];
                    $FansStatus = $this->FansModel->isUpdate(true)->save($FansData);
                }else{
                    $FansData = [
                        'busid' => $concern,
                        'fans' => implode(',',$fans_list)
                    ];
                    $FansStatus = $this->FansModel->save($FansData);
                }
                // var_dump($FansData);
                // exit;

                $ConcernStatus = $this->BusinessModel->isUpdate(true)->save($ConcernData);

                if($ConcernStatus === FALSE){
                    $this->error($this->BusinessModel->getError());
                    exit;
                }

               
                if($FansStatus === FALSE){
                    $this->BusinessModel->rollback();
                    $this->error($this->FansModel->getError());
                    exit;
                }

                if($ConcernStatus === FALSE || $FansStatus === FALSE){
                    $this->BusinessModel->rollback();
                    $this->FansModel->rollback();
                    $this->error('关注失败');
                    exit;
                }else{
                    $business = $this->BusinessModel->find($this->BusinessModel->id);
                    $this->BusinessModel->commit();
                    $this->FansModel->commit();
                    $this->success('关注成功',null,$business);
                    exit;
                }
            }
        }
    }


    // 关注列表获取
    public function concerndata(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $page = $this->request->param('page',1,'trim');
            $keywords = $this->request->param('keywords','','trim');
            $limit = 10;
            $offset = ($page-1)*$limit;


            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            $where = [
                'id'=>['IN',$business['concern_list']]
            ];

            if($keywords){
                $where['nickname'] = ['LIKE',"%$keywords%"];
            }


            $list = $this->BusinessModel
                ->where($where)
                ->limit($offset,$limit)
                ->select();
            

            if(!$list){
                $this->error('暂无更多关注用户');
                exit;
            }else{
                $this->success('返回关注用户',null,$list);
                exit;
            }

        }
    }


    // 粉丝列表获取
    public function fansdata(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $page = $this->request->param('page',1,'trim');
            $keywords = $this->request->param('keywords','','trim');
            $limit = 10;
            $offset = ($page-1)*$limit;


            // 查询用户
            $business = $this->FansModel->where(['busid' => $busid])->find();
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            $where = [
                'id'=>['IN',$business['fans']]
            ];

            if($keywords){
                $where['nickname'] = ['LIKE',"%$keywords%"];
            }


            $list = $this->BusinessModel
                ->where($where)
                ->limit($offset,$limit)
                ->select();
            

            if(!$list){
                $this->error('暂无更多粉丝');
                exit;
            }else{
                $this->success('返回粉丝',null,$list);
                exit;
            }

        }
    }

    
    // 发送私信
    public function send(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $accept = $this->request->param('accept',0,'trim');
            $content = $this->request->param('content','','trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
            }

            $acceptbus = $this->BusinessModel->find($accept);
            if(!$acceptbus){
                $this->error('用户未知');
            }


            $data = [
                'busid' => $busid,
                'accept' => $accept,
                'status' => 0,
                'content' => $content
            ];

            $result = $this->MsgModel->save($data);

            if($result === FALSE){
                $this->error('发送失败');
                exit;
            }else{
                $this->success('发送成功');
                exit;
            }
        }
    }


    // 查看私信列表
    public function msg(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $page = $this->request->param('page',1,'trim');
            $keywords = $this->request->param('keywords','','trim');
            // 查找用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            $where = [
                'accept' => $busid
            ];

            if($keywords){
                $sendbus = $this->BusinessModel->where(['nickname' => ['LIKE',"%$keywords%"]])->find();

                $where['busid'] = $sendbus['id'];
            }

            // 查找私信
            $list = $this->MsgModel
                ->with(['business'])
                ->where($where)
                ->select();

            if(!$list){
                $this->error('暂无更多私信');
                exit;
            }else{
                $this->success('返回私信',null,$list);
                exit;
            }
        }
    }


    // 查看私信人
    public function infobus(){
        if($this->request->isPost()){
            $msgid = $this->request->param('msgid',0,'trim');

            $msg = $this->MsgModel->find($msgid);
            if(!$msg){
                $this->error('私信信息未知');
                exit;
            }

            $business = $this->BusinessModel->find($msg['busid']);

            if(!$business){
                $this->error('用户信息未知');
                exit;
            }else{
                $this->success('返回用户',null,$business);
                exit;
            }
        }
    }


    // 查看私信详细信息
    public function msginfo(){
        if($this->request->isPost()){
            $msgid = $this->request->param('msgid',0,'trim');

            // 查询私信
            $msg = $this->MsgModel->find($msgid);
            if(!$msg){
                $this->error('私信内容未知');
                exit;
            }else{
                $this->success('返回私信',null,$msg);
            }
        }
    }


    // 已读
    public function read(){
        if($this->request->isPost()){
            $msgid = $this->request->param('msgid',0,'trim');

            // 查询私信
            $msg = $this->MsgModel->find($msgid);
            if(!$msg){
                $this->error('私信信息未知');
                exit;
            }

            $data = [
                'id' => $msgid,
                'status' => 1
            ];

            $result = $this->MsgModel->isUpdate(true)->save($data);

            if($result === FALSE){
                $this->error('状态修改失败');
                exit;
            }else{
                $this->success('状态修改成功');
                exit;
            }
        }
    }


    // 删除私信
    public function msgdel(){
        if($this->request->isPost()){
            $msgid = $this->request->param('msgid',0,'trim');

            // 查询私信
            $msg = $this->MsgModel->find($msgid);
            if(!$msg){
                $this->error('私信信息未知');
                exit;
            }

            $result = $this->MsgModel->where(['id'=>$msgid])->delete();

            if($result === FALSE){
                $this->error('删除失败');
                exit;
            }else{
                $this->success('删除成功');
                exit;
            }
        }
    }

}
